package com.classroom.smart.controller;

import com.classroom.smart.pojo.Recruit;
import com.classroom.smart.pojo.Teacher;
import com.classroom.smart.result.CommonResult;
import com.classroom.smart.service.RecruitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class RecruitController {

    @Autowired
    private RecruitService recruitService;
    @PostMapping("onSubmit")
    @ResponseBody
    public CommonResult onSubmit(@RequestBody Recruit recruit){
        CommonResult commonResult = null;
        List<Recruit> recruits = recruitService.selectByNamePersonid(recruit.getName(),recruit.getPersonid());
        int flag = 0;
        for(int i = 0; i < recruits.size(); i++){
            if (recruits.get(i) == recruit){
                flag = 1;
                break;
            }
        }
        if (flag == 1){
            commonResult = new CommonResult(433,"已有已提交的申请");
        }else {
            boolean anotherFlag = recruitService.addOne(recruit);
            if (anotherFlag){
                commonResult = new CommonResult(200,"申请成功",flag);
            }else {
                commonResult = new CommonResult(499,"申请失败");
            }
        }
        return commonResult;
    }


}
